package eseo.avion;

import java.sql.*;
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class HoraireServlet
 */
@WebServlet("/RechServ")
public class HoraireServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HoraireServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String numeroVol = request.getParameter("num_vol");
		String dateDepart1 = request.getParameter("date_vol");
		String depart_lieu = request.getParameter("ville_depart");
		String arrivee_lieu = request.getParameter("ville_arrivee");
		String dateDepart2 = request.getParameter("date_ville");
		String choix = request.getParameter("choix");

		try {
			if(choix.equals("numero_vol")){
			
			DriverManager.registerDriver(new com.mysql.jdbc.Driver());
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/agence_voyage?user=arthur&password=arthur");
			Statement stmt = conn.createStatement();
			String sql_query = "SELECT * FROM vol WHERE num_vol = '" + numeroVol + "' AND date = '" + dateDepart1 + "' ORDER BY date, depart_heure;";
			System.out.println(sql_query);
			stmt.execute(sql_query.toString());
			//System.out.println("step A");
			ResultSet rset = stmt.getResultSet();
			Vol[] volHoraire1 = new Vol[10];
			int i=0;
			
			if(!rset.next()){
				RequestDispatcher dispat = request.getRequestDispatcher("ErreurHoraire.jsp");
				dispat.forward(request, response);
			}
			
			else{
				rset.previous();
				while(rset.next()){
					volHoraire1[i] = new Vol();
					volHoraire1[i].setNumVol(rset.getString(1));
					volHoraire1[i].setDateVol(rset.getDate(2).toString());
					volHoraire1[i].setDepartLieu(rset.getString(3));
					volHoraire1[i].setArriveeLieu(rset.getString(4));
					volHoraire1[i].setDepartHeure((rset.getTime(5)).toString());
					volHoraire1[i].setArriveeHeure((rset.getTime(6)).toString());
					volHoraire1[i].setPrix(rset.getInt(7));
					i++;	 
				}
				session.setAttribute("volHoraire1", volHoraire1);
				session.setAttribute("choix", choix);
				RequestDispatcher dispat = request.getRequestDispatcher("Horaire.jsp");
				dispat.forward(request, response);
			}
			}
			else if(choix.equals("ville")){
				Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/agence_voyage?user=arthur&password=arthur");
				Statement stmt_2 = conn.createStatement();
				String sql_query_2 = "SELECT * FROM vol WHERE depart_lieu = '" + depart_lieu + "' AND arrivee_lieu = '" + arrivee_lieu + "' AND date = '" + dateDepart2 + "' ORDER BY date, depart_heure;";
				System.out.println(sql_query_2);
				stmt_2.execute(sql_query_2.toString());
				System.out.println("step B");
				ResultSet rset_2 = stmt_2.getResultSet();
				Vol[] volHoraire2 = new Vol[10];
				int j=0;
				
				if(!rset_2.next()){
					RequestDispatcher dispat = request.getRequestDispatcher("ErreurHoraire.jsp");
					dispat.forward(request, response);
				}
				
				else{
					rset_2.previous();
					while(rset_2.next()){
						volHoraire2[j] = new Vol();
						volHoraire2[j].setNumVol(rset_2.getString(1));
						volHoraire2[j].setDateVol(rset_2.getDate(2).toString());
						volHoraire2[j].setDepartLieu(rset_2.getString(3));
						volHoraire2[j].setArriveeLieu(rset_2.getString(4));
						volHoraire2[j].setDepartHeure((rset_2.getTime(5)).toString());
						volHoraire2[j].setArriveeHeure((rset_2.getTime(6)).toString());
						volHoraire2[j].setPrix(rset_2.getInt(7));
						j++;	 
					}
					session.setAttribute("volHoraire2", volHoraire2);
					session.setAttribute("choix", choix);
					RequestDispatcher dispat = request.getRequestDispatcher("Horaire.jsp");
					dispat.forward(request, response);
				}
			}
				
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
}
}
